package com.game.business.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import com.game.business.domain.TCouponUser;
import com.game.business.service.ITCouponUserService;
import com.game.common.annotation.Log;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import com.game.common.core.domain.entity.SysUser;
import com.game.common.enums.BusinessType;
import com.game.common.utils.poi.ExcelUtil;
import javax.servlet.http.HttpServletResponse;

import com.game.system.service.ISysUserService;
import com.game.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.game.business.domain.TCoupon;
import com.game.business.service.ITCouponService;
import com.game.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author ruoyi
 * @date 2025-10-11
 */
@RestController
@RequestMapping("/business/coupon")
public class TCouponController extends BaseController {
    @Autowired
    private ITCouponService tCouponService;


    @Autowired
    private ITCouponUserService tCouponUserService;

    @Autowired
    private ISysUserService sysUserService;


    /**
     * 查询优惠券列表
     */
//@RequiresPermissions("business:coupon:list")
    @GetMapping("/list")
    public TableDataInfo list(TCoupon tCoupon) {
        startPage();
        List<TCoupon> list = tCouponService.selectTCouponList(tCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    //@RequiresPermissions("business:coupon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCoupon tCoupon) {
        List<TCoupon> list = tCouponService.selectTCouponList(tCoupon);
        ExcelUtil<TCoupon> util = new ExcelUtil<TCoupon>(TCoupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    //@RequiresPermissions("business:coupon:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(tCouponService.getById(id));
    }

    /**
     * 新增优惠券
     */
    //@RequiresPermissions("business:coupon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCoupon tCoupon) {
        return toAjax(tCouponService.save(tCoupon));
    }

    /**
     * 修改优惠券
     */
    //@RequiresPermissions("business:coupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCoupon tCoupon) {
        return toAjax(tCouponService.updateById(tCoupon));
    }

    /**
     * 删除优惠券
     */
    //@RequiresPermissions("business:coupon:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tCouponService.removeByIds(Arrays.asList(ids)));
    }



    @PostMapping("/sendUserCoupon")
    public AjaxResult sendUserCoupon(@RequestBody TCouponUser vo) {
        String taskId = UUID.randomUUID().toString();
        TCoupon coupon = tCouponService.getById(vo.getId());
        //理后台维护的，可以发给全体用户(用户如果有担保人身份就不包括)，也只有创建订单的时候可以用
        CompletableFuture<Void> task =   CompletableFuture.runAsync(() -> {
            // 执行原逻辑
            SysUser sysUser = new SysUser();
            List<SysUser> sysUsers = sysUserService.selectUserListApp(sysUser);
//            List<SysUser> sysUserList = sysUsers.stream()
//                    .filter(sysUser1 -> !sysUser1.getIsDls().equals("1"))
//                    .collect(Collectors.toList());
            List<TCouponUser> tCouponUserList = new ArrayList<>();
            for (SysUser sysUser1 : sysUsers) {
                String isDbr = sysUser1.getIsDbr();
                if(!"1".equals(isDbr)){
                    TCouponUser tCouponUser1 = new TCouponUser();
                    tCouponUser1.setTitle(coupon.getTitle());
                    tCouponUser1.setCouponId(vo.getId());
                    tCouponUser1.setUserId(sysUser1.getUserId() + "");
                    tCouponUser1.setMoney(vo.getMoney());
                    tCouponUser1.setStatus("0");
                    tCouponUserList.add(tCouponUser1);
                }
            }
            if(!CollectionUtils.isEmpty(tCouponUserList)){
                tCouponUserService.saveBatch(tCouponUserList);
            }
        });

        // 返回任务ID给前端
        return AjaxResult.success("优惠券已经发送 ");
    }
}

